package com.skt.common_hr.medicalmoney.model
{
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.medicalmoney.model.vo.MedicalMoneyVO;
	import com.skt.common_hr.medicalmoney.model.vo.MedicalSubTabVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
   
	public class MedicalMoneyApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "MedicalMoneyApprovalProxy";

		public var dataProvider:MedicalMoneyVO;
		public var acDataProvider2:ArrayCollection;

		private var templateHTML:String = "";
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function MedicalMoneyApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}

		private var _key:String = "";
		public function get key():String
		{
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}


		public function get title():String
		{
			var tmp:String = "의료비 신청 (";
			var day:String = String(dataProvider.SPMON).substring(0,4)+'년 ' + String(dataProvider.SPMON).substring(4,6)+'월'
			var howmuch:String = ', 신청금액:'+NumberUtil.NumberFormat(dataProvider.HOWMUCH)+'원)';
			//tmp += " (" + dataProvider.ZTEXT + ")"
			return tmp + day + howmuch;
		}

		public function get content():String
		{
			return buildHTML(dataProvider, acDataProvider2);
		}

		public function get appCode():String
		{
			return "11"; 	//
		}


 		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/medicalmoney.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}

		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}

 		private function buildHTML(vo:MedicalMoneyVO, ac:ArrayCollection):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";

			var ptnCname:RegExp  	= /__EMFTX__/;
			var ptnIDATE_F:RegExp  	= /__IDATE_F__/;
			var ptnSPMON:RegExp  	= /__SPMON__/;
			var ptnHOWMANY:RegExp  	= /__HOWMANY__/;
			var ptnHOWMUCH:RegExp  	= /__HOWMUCH__/;
			var ptnHOWMUCHALL:RegExp  	= /__HOWMUCH_ALL__/;
			var ptnMedit:RegExp  	= /__MEDIT__/;
			
			var ptnAC:RegExp  	= /__AC__/;
			
			
			//var ptnFtext:RegExp  = /__FTEXT__/;
			//var ptnCosten:RegExp = /__COSTEN__/;

			htmlAll = htmlAll.replace(ptnMedit, vo.MEDIT);
			
			htmlAll = htmlAll.replace(ptnCname, vo.EMFTX);
			htmlAll = htmlAll.replace(ptnIDATE_F, vo.USEDT0);
			htmlAll = htmlAll.replace(ptnSPMON, String(vo.SPMON).substring(0,4)+'년 ' + String(vo.SPMON).substring(4,6)+'월'  );
			htmlAll = htmlAll.replace(ptnHOWMANY, vo.HOWMANY + '건');
			htmlAll = htmlAll.replace(ptnHOWMUCH, NumberUtil.NumberFormat(vo.HOWMUCH) +'원');
			htmlAll = htmlAll.replace(ptnHOWMUCHALL, NumberUtil.NumberFormat(vo.HOWMUCH_ALL) +'원');

			var sTagTd1:String = "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30>"; 
			var sTagTd2:String = "<TD style='background: #fdfdf3; text-align: right; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30>";

			var oSelectedItem:MedicalSubTabVO;
			var sSub:String="";
			for(var i:int; i<ac.length; i++) {				
				oSelectedItem = ac.getItemAt(i) as MedicalSubTabVO;
				sSub = sSub + "<TR>"
				sSub = sSub + sTagTd1+"&nbsp;"+DateUtil.toDotString(oSelectedItem.USEDT)+"</TD>";
				sSub = sSub + sTagTd1+"&nbsp;"+oSelectedItem.HOSPD+"</TD>";
				sSub = sSub + sTagTd2+"&nbsp;"+oSelectedItem.BETRG_F+"원</TD>";
				sSub = sSub + "</TR>"
			}			
			
			htmlAll = htmlAll.replace(ptnAC, sSub);
			
//			if(vo.COSEL == 'X') {
//				htmlAll = htmlAll.replace(ptnCosten, vo.COSTEN);
//			} else {
//				htmlAll = htmlAll.replace(ptnCosten, '');
//			}
			return htmlAll;
		}
	}
}